//
//  UserAccount.swift
//  Weibo10
//
//  Created by Leon on 15/10/17.
//  Copyright © 2015年 Leon. All rights reserved.
//

import UIKit

class UserAccount: NSObject, NSCoding {
    
    // 用户昵称
    var screen_name: String?
    //用户头像地址（大图），180×180像素
    var avatar_large: String?
    // 过期时间
    var expireData: NSDate?
    
    // 用于调用access_token，接口获取授权后的access token
    var access_token: String?
    // access_token的生命周期，单位是秒数
    // typealias NSTimeInterval = Double
    var expires_in: NSTimeInterval = 0{
        didSet {
            expireData = NSDate(timeIntervalSinceNow: expires_in)
        }
    }
    //当前授权用户的UID
    var uid: String?
    
    init(dict: [String: AnyObject]) {
        super.init()
        setValuesForKeysWithDictionary(dict)
    }
    
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {}
    
    override var description: String {
        
        let keys = ["access_token", "expires_in", "uid", "screen_name", "avatar_large", "expireData"]
        return dictionaryWithValuesForKeys(keys).description
    }
    
    /**
    归档方法
    
    - parameter aCoder: 编码器
    */
    func encodeWithCoder(aCoder: NSCoder) {
        aCoder.encodeObject(access_token, forKey: "access_token")
        aCoder.encodeObject(expireData, forKey: "expireData")
        aCoder.encodeObject(uid, forKey: "uid")
        aCoder.encodeObject(screen_name, forKey: "screen_name")
        aCoder.encodeObject(avatar_large, forKey: "avatar_large")
    }
    
    /**
    解档方法
    
    - parameter aDecoder: 解码器
    
    - returns: 用户对象
    */
    required init?(coder aDecoder: NSCoder) {
        access_token = aDecoder.decodeObjectForKey("access_token") as? String
        expireData = aDecoder.decodeObjectForKey("expireData") as? NSDate
        uid = aDecoder.decodeObjectForKey("uid") as? String
        screen_name = aDecoder.decodeObjectForKey("screen_name") as? String
        avatar_large = aDecoder.decodeObjectForKey("avatar_large") as? String
    }
    // required 注意：指定构造函数不能在 extension 中实现
    

}
